﻿using System;
using uTILLIty.ADExtractor.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.DirectoryServices;

namespace uTILLIty.ADExtractor.CoreTests
{
	/// <summary>
	///This is a test class for ActiveDirectoryExtractorTest and is intended
	///to contain all ActiveDirectoryExtractorTest Unit Tests
	///</summary>
	[TestClass]
	public class ActiveDirectoryExtractorTest : ActiveDirectoryExtractorTestBase
	{

		/// <summary>
		///A test for QueryActiveDirectory
		///</summary>
		[TestMethod]
		public void QueryActiveDirectoryTest()
		{
			var target = new ActiveDirectoryExtractor();
			var query = new QueryInformation
			{
				Query = "(&(objectCategory=User)(initials=*)(sn=A*))",
				PropertiesToLoad = new[] { new AdPropertyInfo("Initials"),
					new AdPropertyInfo("GivenName"), 
					new AdPropertyInfo("sn"),
					new AdPropertyInfo("DisplayName"), 
					new AdPropertyInfo("sAMAccountName"), 
					new AdPropertyInfo("department"), 
					new AdPropertyInfo("objectSID")
				}
			};
			SearchResultCollection actual = target.QueryActiveDirectory(query);
			foreach (SearchResult item in actual)
			{
				TestContext.WriteLine("{0}\n", item.Path);
				foreach (var prop in query.PropertiesToLoad)
				{
					TestContext.WriteLine("\t{0}={1}\n", prop, ActiveDirectoryExtractor.GetValue(item, prop, query));
				}
			}
		}

		[TestMethod]
		public void CountAggregationTest()
		{
			var target = new ActiveDirectoryExtractor();
			var query = new QueryInformation
			{
				Query = "(&(objectClass=Group)(cn=Administrators))",
				PropertiesToLoad = new[] { AdPropertyInfo.Parse("count(member)"), new AdPropertyInfo("cn") }
			};
			SearchResultCollection actual = target.QueryActiveDirectory(query);
			PrintResult(query, actual);
		}

		[TestMethod]
		public void NameOfManagerTest()
		{
			var target = new ActiveDirectoryExtractor();
			var query = new QueryInformation
			{
				Query = string.Format("(&(objectClass=User)(samAccountName={0}))", Environment.GetEnvironmentVariable("USERNAME")),
				PropertiesToLoad = new[]
					                   {
															 new AdPropertyInfo("cn"), 
															 new AdPropertyInfo("manager"),
															 new AdPropertyInfo("manager.cn"), 
															 new AdPropertyInfo("manager.title"), 
															 new AdPropertyInfo("manager.objectsid"), 
															 new AdPropertyInfo("manager.manager.cn"), 
															 new AdPropertyInfo("manager.manager.manager.cn")
					                   }
			};
			SearchResultCollection actual = target.QueryActiveDirectory(query);
			PrintResult(query, actual);
		}

		[TestMethod]
		public void AllPipedAggregationTest()
		{
			var target = new ActiveDirectoryExtractor();
			var query = new QueryInformation
			{
				Query = "(&(objectClass=Group)(cn=Administrators))",
				PropertiesToLoad = new[] { AdPropertyInfo.Parse("All(member)"),
					new AdPropertyInfo("cn")
				}
			};
			SearchResultCollection actual = target.QueryActiveDirectory(query);
			PrintResult(query, actual);
		}


		[TestMethod]
		public void CountAndAllPipedAggregationTest()
		{
			var target = new ActiveDirectoryExtractor();
			var query = new QueryInformation
			{
				Query = "(&(objectClass=Group)(cn=Administrators))",
				PropertiesToLoad = new[] { AdPropertyInfo.Parse("count(member)"), AdPropertyInfo.Parse("All(member)"), new AdPropertyInfo("cn") }
			};
			SearchResultCollection actual = target.QueryActiveDirectory(query);
			PrintResult(query, actual);
		}
	}
}
